package com.example.controller;


import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.common.Result;
import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    @GetMapping("/selectAll")
    public Result selectAll(User user) {
        return Result.success(userService.selectAll(user));
    }

    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "3") Integer pageSize,
                             User user) {
        return Result.success(userService.selectPage(pageNum, pageSize, user));
    }

    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        userService.add(user);
        return Result.success("success");
    }

    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        userService.update(user);
        return Result.success("success");
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        userService.deleteById(id);
        return Result.success("success");
    }

    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<User> userList) {
        userService.deleteBatch(userList);
        return Result.success("success");
    }

    @GetMapping("/export")
    public void export(User user, HttpServletResponse response) throws Exception {
        String ids = user.getIds();

        if(StrUtil.isNotBlank(ids)){
            String[] split = ids.split(",");
            user.setIdsArr(split);
        }

        List<User> userList = userService.selectAll(user);
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.addHeaderAlias("name", "姓名");
        writer.addHeaderAlias("username", "账号");
        writer.addHeaderAlias("phone", "手机号");
        writer.addHeaderAlias("email", "邮箱");
        writer.setOnlyAlias(true);
        writer.write(userList);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String filename = URLEncoder.encode("普通用户信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=" + filename + ".xlsx");
        ServletOutputStream outputStream = response.getOutputStream();
        writer.flush(outputStream);
        writer.close();
        outputStream.close();
    }

    @PostMapping("/import")
    public Result importData(@RequestParam("file") MultipartFile file) throws Exception {
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        reader.addHeaderAlias("姓名", "name");
        reader.addHeaderAlias("账号", "username");
        reader.addHeaderAlias("手机号", "phone");
        reader.addHeaderAlias("邮箱", "email");

        List<User> list = reader.readAll(User.class);
        for (User user : list) {
            userService.add(user);
        }
        return Result.success("success");

    }


}
